package com.lyk.read.book.ui.detail

import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lyk.common.base.BaseDialog
import com.lyk.common.dialog.NormalDialog
import com.lyk.common.util.AppDataUtil
import com.lyk.common.util.GsonUtil.jsonToList
import com.lyk.common.util.GsonUtil.toJson
import com.lyk.read.book.R
import com.lyk.read.book.base.BaseBookActivity
import com.lyk.read.book.bean.DetailItemBean
import com.lyk.read.book.bean.ItemBean
import com.lyk.read.book.dialog.AddOrEditDialog
import com.lyk.read.book.ui.main.MainActivity

/**
 * 复制图片到本地目录，删除图片，或直接拍照到本地
 *
 * Created by LianYongKe on 2024/3/15.
 */
class DetailActivity : BaseBookActivity() {

    private val recyclerView: RecyclerView by lazy { findViewById(R.id.detailRecycler) }

    private val adapter: DetailAdapter by lazy { DetailAdapter(list) }

    private lateinit var saveKey: String
    private val list: MutableList<DetailItemBean> = mutableListOf()

    override fun getLayoutId(): Int {
        return R.layout.activity_detail
    }

    override fun initView() {
        val item = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getParcelableExtra("list_item", ItemBean::class.java)
        } else {
            @Suppress("DEPRECATION") intent.getParcelableExtra("list_item")
        }
        findViewById<TextView>(R.id.detailTitle).text = item?.title
        saveKey = item?.title ?: ""

        recyclerView.layoutManager = LinearLayoutManager(mContext)
        recyclerView.adapter = adapter

        initEvent()
    }

    private fun initEvent() {
        findViewById<Button>(R.id.detailAdd).setOnClickListener {
            AddOrEditDialog().apply {
                arguments = Bundle().apply {
                    putInt("type", 2)
                }
                detailConfirmCallBack = { bean ->
                    list.add(bean)
                    adapter.notifyItemInserted(list.size - 1)
                    AppDataUtil.putString(saveKey, list.toJson())
                }
                show(supportFragmentManager, "")
            }
        }

        adapter.setOnItemLongClickListener { _, _, position ->
            val dialog = NormalDialog().apply {
                arguments = Bundle().apply {
                    putString(NormalDialog.KEY_TITLE, "提示")
                    putString(NormalDialog.KEY_CONTENT, "是否要删除该条记录")
                }
                onConfirmCallBack = object : BaseDialog.ConfirmCallBack {
                    override fun onConfirm(value: String) {
                        list.removeAt(position)
                        adapter.notifyItemRemoved(position)
                        AppDataUtil.putString(saveKey, list.toJson())
                    }
                }
                isCancelable = false // 对话框不可取消
            }
            dialog.show(supportFragmentManager, "")
            true
        }
    }

    override fun initData() {
        super.initData()
        AppDataUtil.getString(saveKey).jsonToList(DetailItemBean::class.java)?.let {
            list.addAll(it)
            adapter.notifyDataSetChanged()
        }
    }

}